/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.projectRPG.src;

import com.jme3.asset.AssetManager;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.PhysicsTickListener;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.PhysicsCollisionObject;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.objects.PhysicsGhostObject;
import com.jme3.bullet.objects.PhysicsRigidBody;
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh.Type;
import com.jme3.effect.shapes.EmitterSphereShape;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import java.io.IOException;
import java.util.Iterator;
import com.jme3.scene.Spatial;
/**
 *
 * @author normenhansen
 */
public class InteractableControl extends RigidBodyControl implements PhysicsCollisionListener, PhysicsTickListener  {

    Main main;
    boolean underwater = false;
    private float density;
    
    public InteractableControl(CollisionShape shape, float massValue, float densityValue, Main app) {
        super(shape, massValue);
        density = densityValue;
        main = app;
        underwater = getIsUnderwater();
        main.bulletAppState.getPhysicsSpace().addCollisionListener(this);
        main.bulletAppState.getPhysicsSpace().addTickListener(this);
        // make shape slightly bigger
        //ghostObject = new PhysicsGhostObject(shape);
        
        
    }

    public void collision(PhysicsCollisionEvent event) {
        if (space == null) {
            return;
        }
        
       //  main.player.carriedObject = null;
        /*
        System.out.println(event.getObjectA());
        System.out.println(event.getObjectB());
        System.out.println("Impact: " + event.getAppliedImpulse());
*/
    }
    
    private boolean getIsUnderwater()
    {
        if(getPhysicsLocation().y > main.currentArea.waterLevel)
        {
            return false;
        }    
        return true;
    }
    
    @Override
    public void prePhysicsTick(PhysicsSpace space, float f){
        underwater = getIsUnderwater();        
        if(underwater)
        {
            float force = mass / density * 4f;
            System.out.println(force);
             

        if(getPhysicsLocation().y > main.currentArea.waterLevel - 0.2f)
        {
            this.applyCentralForce(new Vector3f (0, force * .3f, 0)); 
        }else{
            this.applyCentralForce(new Vector3f (0, force, 0));            
        }
            //this.setFriction(1.0f);

        }else{
            //this.setDamping(0.0f, 0.0f);
            //this.setFriction(0.1f);    

        }
    }  
    
    @Override
    public void physicsTick(PhysicsSpace space, float f) {
 
    }    
  
}

